package com.niguang.work.labour.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.niguang.work.labour.api.entity.AdminLaborCompanySite;
import com.niguang.work.labour.api.entity.AppUser;
import com.niguang.work.labour.api.entity.TeamUser;
import com.niguang.work.labour.api.entity.Type;
import com.niguang.work.labour.api.mapper.AdminLaborCompanySiteMapper;
import com.niguang.work.labour.api.mapper.TypeMapper;
import com.niguang.work.labour.api.service.AdminLaborCompanySiteService;
import com.niguang.work.labour.api.service.AppUserService;
import com.niguang.work.labour.api.service.TeamUserService;
import com.niguang.work.labour.api.service.TypeService;
import com.niguang.work.labour.common.constant.ResultCode;
import com.niguang.work.labour.common.pojo.Result;
import com.niguang.work.labour.common.util.ApiTokenUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 劳务公司工地关联关系表 服务实现类
 * </p>
 *
 * @author 83673
 * @since 2018-11-17
 */
@Service
public class AdminLaborCompanySiteServiceImpl extends ServiceImpl<AdminLaborCompanySiteMapper, AdminLaborCompanySite> implements AdminLaborCompanySiteService {
    @Autowired
    TeamUserService workTeamUserService;
    @Autowired
    private ApiTokenUtils apiTokenUtils;
    @Autowired
    private TypeMapper workTypeMapper;
    @Autowired
    private TypeService workTypeService;
    @Autowired
    private AppUserService workAppUserService;

    @Override
    public Result searchLabor(String siteName) {
        Long userId = apiTokenUtils.getUserId();
        return Result.build(ResultCode.SUCCESS, "项目搜索成功",
                baseMapper.searchLabor(siteName, userId));
    }

    @Override
    public Result searchSiteLabor(Long siteId) {
        Long userId = apiTokenUtils.getUserId();
        QueryWrapper<TeamUser> entityWrapper = new QueryWrapper<>();
        entityWrapper.eq(TeamUser.USER_ID, userId).eq(TeamUser.DEL_FLAG, 0).eq(TeamUser.SITE_ID, siteId);
        List<TeamUser> teamUserList = workTeamUserService.list(entityWrapper);
        if (CollectionUtils.isEmpty(teamUserList)) {
            return Result.build(ResultCode.SUCCESS, teamUserList);
        }
        List<Map<String, Object>> list = new ArrayList<>(teamUserList.size());
        teamUserList.forEach(p -> {
            Type workType = workTypeMapper.selectById(p.getWorkTypeId());
            Map<String, Object> typeMap = new HashMap<>(3);
            typeMap.put("teamId", p.getId());
            typeMap.put("bzName", p.getBzName());
            typeMap.put("workTypeName", workType.getName());
            list.add(typeMap);
        });
        return Result.build(ResultCode.SUCCESS, "项目搜索成功", list);
    }

    @Override
    public Result getWorkType() {
        QueryWrapper<Type> ew = new QueryWrapper<>();
        ew.eq(Type.DEL_FLAG, 0);
        List<Type> typeList = workTypeService.list(ew);
        if (CollectionUtils.isEmpty(typeList)) {
            return Result.build(ResultCode.SUCCESS, typeList);
        }
        List<Map<String, Object>> list = new ArrayList<>(typeList.size());
        typeList.forEach(p -> {
            Map<String, Object> typeMap = new HashMap<>(2);
            typeMap.put("workTypeId", p.getId());
            typeMap.put("workTypeName", p.getName());
            list.add(typeMap);
        });
        return Result.build(ResultCode.SUCCESS, list);
    }

    @Override
    public Result getLaborUser(String userPhone) {
        QueryWrapper<AppUser> entityWrapper = new QueryWrapper<>();
        entityWrapper.like(AppUser.USER_PHONE, userPhone)
                .eq(AppUser.DEL_FLAG, 0).isNotNull(AppUser.FACE_ID).last("limit 10");
        List<AppUser> userList = workAppUserService.list(entityWrapper);
        if (CollectionUtils.isEmpty(userList)) {
            return Result.build(ResultCode.SUCCESS, "无用户");
        }
        List<Map<String, Object>> list = new ArrayList<>();
        userList.parallelStream().forEach(p -> {
            Map<String, Object> params = new HashMap<>(3);
            params.put("userId", p.getId());
            params.put("userName", p.getUserName());
            params.put("userPhone", p.getUserPhone());
            list.add(params);
        });
        return Result.build(ResultCode.SUCCESS, "搜索人员查询成功！", list);
    }
}
